<?
define ("table_banners","banners");

$BANNERS_RIGHT_COUNT=5;

function getmicrotime() { 
    list($usec, $sec) = explode(" ", microtime()); 
    return ((double)$usec + (double)$sec); 
}

function show_banner($row) {
	switch ($row["bannerType"]) {
		case 0:
			?><img src="<?=BANNERS_URL.$row["bannerImage"]?>" alt="<?=$row["bannerAlt"]?>" <?if ($row["bannerSizeX"]>0&&$row["bannerSizeY"]>0) {?>width="<?=$row["bannerSizeX"]?>" height="<?=$row["bannerSizeY"]?>"<?}?> border="0" <? if ($row["bannerOnClickType"]=="1"){?> style="cursor:pointer;cursor:hand;" onclick="showA(<?=$row["bannerID"]?>,<?=$row["bannerType"]?>,'<?=$row["bannerOnClickValue"]?>');"<?}?>><?
			break;
		case 1:
			?><OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" <?if ($row["bannerSizeX"]>0&&$row["bannerSizeY"]>0) {?>WIDTH="<?=$row["bannerSizeX"]?>" HEIGHT="<?=$row["bannerSizeY"]?>" <?}?>id="b<?=$row["bannerID"]?>"<? if ($row["bannerOnClickType"]=="1"){?> style="cursor:pointer;cursor:hand;" onclick="showA(<?=$row["bannerID"]?>,<?=$row["bannerType"]?>,'<?=$row["bannerOnClickValue"]?>');"<?}?>>
<PARAM NAME=movie VALUE="<?=BANNERS_URL.$row["bannerImage"]?>">
<PARAM NAME=quality VALUE=high>
<PARAM NAME=bgcolor VALUE=#FFFFFF>

<EMBED src="<?=BANNERS_URL.$row["bannerImage"]?>" quality=high bgcolor=#FFFFFF <?if ($row["bannerSizeX"]>0&&$row["bannerSizeY"]>0) {?>WIDTH="<?=$row["bannerSizeX"]?>" HEIGHT="<?=$row["bannerSizeY"]?>" <?}?>
NAME="myMovieName" ALIGN="" TYPE="application/x-shockwave-flash"
PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">
</EMBED>
</OBJECT><?
			break;
		case 2:
			?><img src="<?=$row["bannerURL"]?>" alt="<?=$row["bannerAlt"]?>" <?if ($row["bannerSizeX"]>0&&$row["bannerSizeY"]>0) {?>width="<?=$row["bannerSizeX"]?>" height="<?=$row["bannerSizeY"]?>" <?}?>border="0" <? if ($row["bannerOnClickType"]=="1"){?> style="cursor:pointer;cursor:hand;" onclick="showA(<?=$row["bannerID"]?>,<?=$row["bannerType"]?>,'<?=$row["bannerOnClickValue"]?>');"<?}?>><?
			break;
		case 3:
			echo $row["bannerScript"];
			break;
	}
}

function show_banners() {
	global $bannerSide;
	global $BANNERS_RIGHT_COUNT;
	$cond = "";
	if( ($bannerSide=='right')||($bannerSide=='left') ){
		$cond = " bannerSide='".(($bannerSide=='left')?"0":"1")."' AND ";
	}

	$qq="SELECT bannerID, bannerURL, bannerImage, bannerAlt, bannerSizeX, bannerSizeY, bannerType, bannerImprLeft, bannerUnlimImpr, bannerOnClickType, bannerOnClickValue FROM ".table_banners.
	" WHERE {$cond} bannerState=1 AND bannerFinishTime>bannerAccess AND ".
	"((bannerUnlimImpr=1 AND bannerImprLeft>0) OR bannerUnlimImpr=0) ORDER BY bannerAccess LIMIT 0,".$BANNERS_RIGHT_COUNT;
//	var_dump($qq);
//	echo "<br>";
	$result=$GLOBALS["db"]->query($qq) or die ("Select failed ".__LINE__);
//	echo "<br>";
	$ids=array();

$count_res = mysql_num_rows($result);
if($count_res>0){?>
<!-- BANNERS-->
	<tr><td valign="top" align="center">
	  <table border="0" cellpadding="0" cellspacing="0">
<?
}
	while ($row=mysql_fetch_array($result,MYSQL_ASSOC)) {
?><tr><td align="center" valign="top"><?
		show_banner($row);
		array_push($ids,$row["bannerID"]);
?></td></tr>
<tr height=5><td></td></tr>
<?
	} // while
if($count_res>0){?>
	  </table>
	</td></tr>
<? }
	$tm=getmicrotime();
	for ($ii=0; $ii<count($ids); $ii++){
		$qq=sprintf("UPDATE ".table_banners." SET bannerAccess='%01.4f', ".
		"bannerImprLeft=IF (bannerUnlimImpr=1,bannerImprLeft-1,bannerImprLeft), ".
		"bannerViewCount=bannerViewCount+1 WHERE bannerID=%d",($tm+$ii/1000),$ids[$ii]);
//		var_dump($qq);
		$GLOBALS["db"]->query($qq) or die ("Update failed ".__LINE__);
	}
}

function banner_frame() {
?><iframe name="hframe" id="hframe" width="0" height="0" style="display: none;"></iframe><?
}?>
